package com.ghsc.ghxc.jsb.ui.my.activity

import android.view.View
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.cn.appcore.adapter.PagerAdapter
import com.cn.appcore.base.BaseVMActivity
import com.cn.appcore.event.RxBus
import com.cn.appcore.event.RxBusBean
import com.flyco.tablayout.listener.OnTabSelectListener
import com.ghsc.ghxc.jsb.R
import com.ghsc.ghxc.jsb.config.CustKey
import com.ghsc.ghxc.jsb.databinding.ActivityOrderBinding
import com.ghsc.ghxc.jsb.ext.intentExtras
import com.ghsc.ghxc.jsb.ui.my.fragment.OrderListFragment
import java.util.ArrayList

/**
 * 项目：GuoHuaJsb
 * 包名：com.ghsc.ghxc.jsb.ui.my.activity
 * 类名：OrderActivity
 * 创建时间：2024/4/25 10:37
 * 创建人 @author： xiaolin
 * 描述 ：Todo...
 */
class OrderActivity  : BaseVMActivity<OrderListViewModel, ActivityOrderBinding>(),
    OnTabSelectListener {
    override fun attachLayoutRes(): Int {
        return  R.layout.activity_order
    }

    override fun attachVMClass(): Class<OrderListViewModel>? {
        return OrderListViewModel::class.java
    }

    override fun initBar() {
        super.initBar()
        mImmersionBar!!.statusBarDarkFont(true).init()
    }

    private val position: Int by intentExtras("position",0)

    var mList: MutableList<String> = ArrayList()
    var mAdapter: PagerAdapter? = null
    private val mFragments: MutableList<Fragment> = ArrayList()
    override fun initView() {
        binding.apply {
            bar.topTitle.text = "订单列表 "
            bar.topLeftView.setOnClickListener { finish() }
            bar.topRightView.visibility = View.GONE
        }
        mList.clear()
        mList.add("待付款")
        mList.add("待发货")
        mList.add("待收货")
        mList.add("已收货")
        mList.add("退换货")

        mFragments.clear()
        OrderListFragment.getInstance(0)?.let { mFragments.add(it) }
        OrderListFragment.getInstance(1)?.let { mFragments.add(it) }
        OrderListFragment.getInstance(3,)?.let { mFragments.add(it) }
        OrderListFragment.getInstance(9)?.let { mFragments.add(it) }
        OrderListFragment.getInstance(4)?.let { mFragments.add(it) }
        mAdapter = PagerAdapter(
            supportFragmentManager,
            mList,
            mFragments
        )
        binding.vpOrder.adapter = mAdapter
        binding.vpOrder.offscreenPageLimit = mList.size - 1
        binding.stlTab.setViewPager(binding.vpOrder)
        binding.stlTab.setOnTabSelectListener(this@OrderActivity)
        binding.stlTab.currentTab = position
        binding.vpOrder.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
            }

            override fun onPageSelected(position: Int) {
                tabIndex(position)
            }

            override fun onPageScrollStateChanged(state: Int) {}
        })

    }

    override fun onTabSelect(position: Int) {
        tabIndex(position)
    }

    override fun onTabReselect(position: Int) {
    }
    private fun tabIndex(index: Int) {

        var orderIndex =0

        if (index ==0) orderIndex=0
        else if (index ==1) orderIndex=1
        else if (index ==2) orderIndex=3
        else if (index ==3) orderIndex=9
        else if (index ==4) orderIndex=4

        RxBus.getDefault().post(RxBusBean(CustKey.ORDER_TAB, CustKey.ORDER_TAB,orderIndex))
    }
}